$(document).ready(function() {
    // 用户名cookie获取
    $("input#userLogin").val(localStorage.getItem("userLogin"));
    // 登录密码cookie获取
    $("input#userPassword").val(localStorage.getItem("userPassword"));
    // 记住密码获取cookie获取
    let rememberPassword = localStorage.getItem("rememberPassword");
    if (rememberPassword == "checked") {
        $('#rememberPassword').prop('checked', true);
    } else {
        $('#rememberPassword').prop('checked', false);
    }
    // 回车自动登录
    $(document).keyup(function(e){
        if(e.key === "Enter"){
            login();
        }
    });
});

function login() {
    //获取账号密码
    let userLogin = $("#userLogin").val();
    let userPassword = $("#userPassword").val();
    //基本校验
    if(!userLogin || !userPassword){
        WeKnow.notifyMsg('用户名和密码不能为空');
        return;
    }
    WeKnow.loading();
    //请求接口
    $.ajax({
        type: "POST",
        url: "login.do/userLogin",
        dataType: "json",
        data: {
            "userLogin":userLogin,
            "userPassword":userPassword
        },
        async: true,//异步调用
        success: function(data) {
            WeKnow.closeLoading();
            if (data.errCode === 0) {
                //登录成功保存信息到cookie
                localStorage.setItem("userLogin", userLogin);
                // 如果记住了密码
                if($('#rememberPassword').prop('checked')){
                    //do something
                    localStorage.setItem("userPassword", userPassword);
                    localStorage.setItem("rememberPassword", "checked");
                } else {
                    localStorage.removeItem("userPassword");
                    localStorage.removeItem("rememberPassword");
                }
                //页面跳转
                WeKnow.location("main");
            } else if (data.errCode === -1 ) {
                WeKnow.errorMsg( data.errMsg );
            } else {
                WeKnow.systemMsg('登录失败，请稍后再试');
            }
        }
    });
}